package ar.com.gals.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

import ar.com.gals.domain.Permiso;
import ar.com.gals.domain.Usuario;


public class SecurityUser implements UserDetails {

	
	private static final long serialVersionUID = 1L;
	
	private Usuario user;
	
	private Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
	
	
	public SecurityUser(Usuario user) {
		
		this.user = user;
		
		for(Permiso permiso : user.getRol().getPermisos())
			authorities.add(new GrantedAuthorityImpl(permiso.getNombre()));
	}

	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return this.authorities;
	}

	@Override
	public String getPassword() {
		return this.user.getPassword();
	}

	@Override
	public String getUsername() {
		return this.user.getNombre();
	}

	@Override
	public boolean isAccountNonExpired() {
		return this.user.isAccountNonExpired();
	}

	@Override
	public boolean isAccountNonLocked() {
		return this.user.isAccountNonLocked();
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return this.user.isCredentialsNonExpired();
	}

	@Override
	public boolean isEnabled() {
		return this.user.isEnabled();
	}
	
	
	/********** METODOS DE DOMINIO *********/
	
	public Usuario getUser() {
		return user;
	}

	public void setUser(Usuario user) {
		this.user = user;
	}
	
	public String getUsuarioConectado() {
		return this.user.usuarioConectado();
	}

	public String getRol() {
		return this.user.getRol().getNombre();
	}
	
	public String getImage() {
		return "/SmallImage/" + ((this.user.getArchivo() == null) ? "no_disponible.jpg" : this.user.getArchivo().getNombre());
	}
}
